package com.wlps.controller;


import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.google.code.kaptcha.impl.DefaultKaptcha;
import com.wlps.contant.Contant;
import com.wlps.entity.SysUser;
import com.wlps.mapper.SysUserMapper;
import com.wlps.result.ResultPageVo;
import com.wlps.result.ResultUtils;
import com.wlps.result.ResultVo;
import com.wlps.service.ISysUserService;
import com.wlps.vo.UserParm;
import io.swagger.annotations.Api;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.scheduling.annotation.Async;
import org.springframework.security.crypto.password.PasswordEncoder;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import javax.imageio.ImageIO;
import javax.servlet.ServletOutputStream;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.awt.image.BufferedImage;
import java.io.IOException;
import java.util.List;

/**
 * <p>
 * 员工表(sys_user) 前端控制器
 * </p>
 *
 * @author ZWYZY
 * @since 2020-07-30
 */
@RestController
@RequestMapping("/api/user")
@Slf4j
@Validated
@Api(tags = "系统管理-员工管理", description = "")
public class SysUserController {
    @Autowired
    private ISysUserService userService;

    @Autowired
    private PasswordEncoder passwordEncoder;
    @Autowired
    SysUserMapper sysUserMapper;

    @Autowired
    RedisTemplate redisTemplate;

    @GetMapping("/getUser")
    public ResultVo<List<SysUser>> getUser() {
        List<SysUser> sysUserList = userService.list();
        if (sysUserList.size() > 0) {
            return ResultUtils.success("成功", sysUserList);
        } else {
            return ResultUtils.error("查询错误", 700);
        }
    }

    /**
     * 新增用户
     *
     * @return
     */
    @RequestMapping(value = "addUser", method = RequestMethod.POST)
    public ResultVo addUser(@RequestBody SysUser user) {
        QueryWrapper<SysUser> query = new QueryWrapper<>();
        query.lambda().eq(SysUser::getUsername, user.getUsername());
//查询用户是否存在
        SysUser one = userService.getOne(query);
        if (one != null) {
            return ResultUtils.error("用户名已经存在!");
        }
//加密用户密码
        String pwd = passwordEncoder.encode(user.getPassword());
        user.setPassword(pwd);
        boolean b = userService.save(user);
        if (b) {

            UserUpdateCache();
            return ResultUtils.success("新增用户成功");
        } else {
            return ResultUtils.error("新增用户失败");
        }
    }

    /**
     * 编辑用户保存
     *
     * @return
     */
    @RequestMapping(value = "updateSaveUser", method = RequestMethod.POST)
    public ResultVo updateSaveUser(@RequestBody SysUser user) {
//判断用户是否存在
        QueryWrapper<SysUser> query = new QueryWrapper<>();
        query.lambda().eq(SysUser::getUsername, user.getUsername());
        SysUser one = userService.getOne(query);
        Long id = one.getId();//查询出来的id
        Long editId = user.getId();//编辑的用户id
        if (!id.equals(editId)) {
            return ResultUtils.error("用户名已经存在!");
        }
        boolean b = userService.updateById(user);
        if (b) {
            UserUpdateCache();
            return ResultUtils.success("编辑成功");
        } else {
            return ResultUtils.error("编辑失败");
        }
    }

    /**
     * 根据用户id删除
     *
     * @return
     */
    @RequestMapping(value = "deleteUserById", method = RequestMethod.GET)
    public ResultVo deleteUserById(@RequestParam Long id) {
        boolean b = userService.removeById(id);
        if (b) {
            UserUpdateCache();
            return ResultUtils.success("删除用户成功");
        } else {
            return ResultUtils.error("删除用户失败");
        }
    }

    /**
     * 根据用户id查询用户端
     *
     * @return
     */
    @RequestMapping(value = "getUserById", method = RequestMethod.GET)
    public ResultVo<SysUser> getUserById(@RequestParam Long id) {
        SysUser sysUser = userService.getById(id);
        return ResultUtils.success("查询成功", sysUser);
    }

    //查询用户列表
    @RequestMapping(value = "/getUserList", method = RequestMethod.POST)
    public ResultPageVo<List<SysUser>> getUserList(@RequestBody UserParm parm) {

        ResultPageVo<List<SysUser>> resultPageVo = userService.selectUser(parm);

        return resultPageVo;
    }

    /**
     * 更新缓存
     */
    @Async
    public void UserUpdateCache() {
        List<SysUser> sysUserList = sysUserMapper.selectUsers();
        redisTemplate.opsForValue().set(Contant.USERS_KEY, sysUserList);

        log.info("所有用户-------更新缓存数据");
    }
}
